package com.atguigu.lease.web.admin.controller.system;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.SystemPost;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.SystemPostService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: liuaojun
 * @CreateTime: 2025-02-22
 * @Description:
 * @Version: 1.0
 */
@Tag(name = "后台用户岗位管理")
@RestController
@RequestMapping("/admin/system/post")
public class SystemPostController {
    @Autowired
    private SystemPostService systemPostService;

    @Operation(summary = "分页获取岗位信息")
    @GetMapping("page")
    public Result<IPage<SystemPost>> page(@RequestParam Integer current, @RequestParam Integer size){
        IPage<SystemPost> systemPostIPage = new Page<>();
        IPage<SystemPost> page = systemPostService.page(systemPostIPage);
        return Result.ok(page);
    }

    @Operation(summary = "保存或更新岗位信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SystemPost systemPost){
        systemPostService.saveOrUpdate(systemPost);
        return Result.ok();
    }

    @Operation(summary = "根据ID删除岗位信息")
    @DeleteMapping("deleteById")
    public Result deleteById(@RequestParam Long id){
        systemPostService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = " 获取全部岗位列表")
    @GetMapping("list")
    public Result<List<SystemPost>> list(){
        List<SystemPost> list = systemPostService.list();
        return Result.ok(list);
    }

    @Operation(summary = "根据ID获取岗位信息")
    @GetMapping("getById")
    public Result<SystemPost> getById(@RequestParam Long id){
        SystemPost systemPost = systemPostService.getById(id);
        return Result.ok(systemPost);
    }

    @Operation(summary = "根据ID修改岗位状态")
    @PostMapping("updateStatusByPostId")
    public Result updateStatusByPostId(@RequestParam Long id,@RequestParam BaseStatus status){
        LambdaUpdateWrapper<SystemPost> systemPostLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        systemPostLambdaUpdateWrapper.eq(SystemPost::getId,id);
        systemPostLambdaUpdateWrapper.set(SystemPost::getStatus,status);
        systemPostService.update(systemPostLambdaUpdateWrapper);
        return Result.ok();
    }
}
